<?php
/**
 * Yii view auto
 *
 * @author luubk
 * @since Jan 19, 2013 - 10:54:11 AM
 */
$auth = Yii::app()->authManager;
?>
<div class="row-fluid">
    <div class="box span12">
        <div class="box-header">
            <h2>
                <i class="icon-user"></i><span class="break"></span>
                <?php echo $this->pageTitle ?>
            </h2>
            <div class="box-icon">
                <a href="#" class="btn-close"><i class="icon-remove"></i></a>
            </div>
        </div>
        <div class="box-content">
            <form method="POST" action="">
                <fieldset>                                    
                    <table class="table table-striped table-mod-2">
                        <thead>
                            <tr>
                                <th><?php echo Yii::t('auth', 'Name') ?></th>
                                <th><?php echo Yii::t('auth', 'Description') ?></th>
                                <th><?php echo Yii::t('auth', 'Info') ?></th>
                            </tr>
                        </thead>
                        <tbody>                                
                            <?php
                            $dir  = $_SERVER['DOCUMENT_ROOT'] . Yii::app()->baseUrl . '/protected/modules/'.$this->module->id;
                            ?>
                            <?php if (is_dir($dir)): ?>
                                <?php $controllers = scandir("$dir/controllers"); ?>
                                <?php foreach ($controllers as $ctrl): ?>
                                    <?php
                                    // Đưa các thư mục có tên là . và .. về rỗng để bỏ qua 
                                    $ctrl = trim($ctrl, ".");
                                    // Bỏ qua các thư mục có tên rỗng
                                    if ($ctrl == '')
                                        continue;

                                    // Kiểm tra file nếu không thuộc dạng AbcController.php thì bỏ qua
                                    if (strpos($ctrl, "Controller.php") === FALSE)
                                        continue;

                                    $ctrlName = str_replace("Controller.php", "", $ctrl);
                                    ?>
                                    <tr>
                                        <td>
                                            <input type="checkbox" name="auth[]" value="<?php echo "$ctrlName.*" ?>"
                                            <?php if ($auth->getAuthItem("$ctrlName.*")) echo 'disabled="disabled" checked="checked"' ?> 
                                                   id="<?php echo $ctrlName ?>" onclick="checkChild('<?php echo $ctrlName ?>')"/>
                                            <label for="<?php echo $ctrlName ?>"
                                            <?php $tag      = $auth->getAuthItem("$ctrlName.*") ? "s" : 'strong'; ?>
                                                   <<?php echo $tag ?> class="label label-success checkbox inline">
                                                   <?php echo "$ctrlName.*" ?>
                                                   </<?php echo $tag ?>>
                                            </label>
                                        </td>
                                        <td><?php echo "Manage $ctrlName" ?></td>
                                        <td><i><?php echo "/controllers/$ctrl" ?></i></td>
                                    </tr>
                                    <?php
                                    if ($ctrlName != 'Auth')
                                        include("$dir/controllers/$ctrl")
                                        ?>
                                    <?php $methods  = get_class_methods(ucfirst($ctrlName) . "Controller"); ?>
                                    <?php foreach ($methods as $method): ?>
                                        <?php
                                        // Bỏ qua các phương thước không thuộc dạng: "actionIndex"
                                        if (!preg_match("/^action[^s]+/i", $method))
                                            continue;

                                        // Cắt lấy phần tên của action
                                        $action = str_replace("action", "", $method);
                                        ?>
                                        <tr>
                                            <td>                                                
                                                <input name="auth[]"  value="<?php echo "$ctrlName.$action" ?>"
                                                       type="checkbox" <?php if ($auth->getAuthItem("$ctrlName.$action")) echo 'disabled="disabled" checked="checked"' ?>
                                                       id="<?php echo "$ctrlName.$action" ?>" class="<?php echo $ctrlName ?>" />
                                                <label for="<?php echo "$ctrlName.$action" ?>"
                                                <?php $tag    = $auth->getAuthItem("$ctrlName.$action") ? "s" : 'strong'; ?>
                                                       <<?php echo $tag ?> class="label checkbox inline">
                                                       <?php echo "$ctrlName.$action" ?>
                                                       </<?php echo $tag ?>>                                                    
                                                </label>
                                            </td>
                                            <td><?php echo "$action $ctrlName" ?></td>
                                            <td><i><?php echo $method ?></i></td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                    <button class="btn btn-primary" type="submit">
                        <?php echo Yii::t('form', 'Save') ?>
                    </button>
                </fieldset>
            </form>
        </div>
    </div>
</div>
<script>
    function checkChild(name)
    {
        if ($("#" + name).attr('checked') == 'checked')
        {
            $("." + name).attr('checked', 'checked');
            $("." + name).parent().addClass('checked');
        }
        else
        {
            $("." + name).removeAttr('checked');
            $("." + name).parent().removeClass('checked');
        }
    }
</script>
